/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.opgea.token.web.controller;

import java.io.IOException;
import java.io.Writer;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.opgea.token.domain.model.TokenStatusReplyModel;
import com.opgea.token.service.TokenService;

/**
 *
 * @author Ramesh
 */
@Controller
@RequestMapping(value="status")
public class StatusController {
 
    @Autowired
    private TokenService tokenService;
    
    @RequestMapping(method= RequestMethod.GET)
    public ModelAndView show(ModelMap model, HttpServletRequest request){
        
        Long organizationId = 0L;
        Integer tokenNo = 0;
        if(request.getParameter("orgId") != null){
            organizationId = new Long(request.getParameter("orgId"));
        }
        if(request.getParameter("tokenNo") != null){
            tokenNo = new Integer(request.getParameter("tokenNo"));
        }
        TokenStatusReplyModel statusModel = tokenService.tokenReply(organizationId, tokenNo);
        if(statusModel.getTokenNo() != null){
            return new ModelAndView("statusReply", "status", statusModel);
        }
        else{
            statusModel.setAdditionalMessage("Not in queue");
            return new ModelAndView("statusReply", "status", statusModel);
        }
    }
    
    @RequestMapping(value="reply", method= RequestMethod.GET)
    public void reply(ModelMap model, HttpServletRequest request,
                                              HttpServletResponse response) throws IOException{
        
        
        String keyword = null;
        String who = null;
        String what = null;
        Long organizationId = 0L;
        Integer tokenNo = 0;
        
        if(request.getParameter("who") != null){
            who = (String)(request.getParameter("who"));
        }
        if(request.getParameter("what") != null){
            what = (String)(request.getParameter("what"));
            String messageArray[] = what.split(" ");
            keyword = messageArray[0];
            organizationId = new Long(messageArray[1]);
            tokenNo = new Integer(messageArray[2]);
        }
        
        System.out.println("OrganizationId: "+organizationId);
        System.out.println("Token No: "+tokenNo);
        
        
        TokenStatusReplyModel statusModel = tokenService.tokenReply(organizationId, tokenNo);
        if(statusModel.getTokenNo() != null){
            //return new ModelAndView("statusReply", "status", statusModel);
        }
        else{
            statusModel.setAdditionalMessage("Not in queue");
            //return new ModelAndView("statusReply", "status", statusModel);
        }
        
        response.setContentType("text/html");
        HttpServletResponseWrapper responseWrapper = new HttpServletResponseWrapper(response);
        Writer out = responseWrapper.getWriter();
        StringBuilder message = new StringBuilder();
        message.append("TKN No.: ");
        message.append(statusModel.getTokenNo());
        message.append(" Status: ");
        message.append(statusModel.getStatus());
        message.append(" Name: ");
        message.append(statusModel.getName());
        message.append(" at ");
        message.append(statusModel.getCompanyName());
        message.append(" for ");
        message.append(statusModel.getDestinationName());
        message.append(" Token Position: ");
        message.append(statusModel.getTokenPositionInQueue());
        message.append(" From: ");
        message.append(statusModel.getFrom());
        out.write(message.toString());
        out.close();
        
        System.out.println("Reply Message: "+message);
        //return new ModelAndView("statusReply", "status", statusModel);
    }
    
    @RequestMapping(value="replyNimbus", method= RequestMethod.GET)
    public void replyNimbus(ModelMap model, HttpServletRequest request,
                                              HttpServletResponse response) throws IOException{
        
        String pno = null;        
        String msg = null;
        String mcc = null;
        String mnc = null;
        String keyword = null;
        Long organizationId = 0L;
        Integer tokenNo = 0;
        
        if(request.getParameter("msg") != null){
            msg = (String)(request.getParameter("msg"));
            String messageArray[] = msg.split(" ");
            keyword = messageArray[0];
            organizationId = new Long(messageArray[1]);
            tokenNo = new Integer(messageArray[2]);
        }
        if(request.getParameter("pno") != null){
            pno = (String)(request.getParameter("pno"));
        }
        if(request.getParameter("mcc") != null){
            mcc = (String)(request.getParameter("mcc"));
        }
        if(request.getParameter("mnc") != null){
            mnc = (String)(request.getParameter("mnc"));
        }
        
        System.out.println("OrganizationId: "+organizationId);
        System.out.println("Token No: "+tokenNo);
        
        
        TokenStatusReplyModel statusModel = tokenService.tokenReply(organizationId, tokenNo);
        if(statusModel.getTokenNo() != null){
            //return new ModelAndView("statusReply", "status", statusModel);
        }
        else{
            statusModel.setAdditionalMessage("Not in queue");
            //return new ModelAndView("statusReply", "status", statusModel);
        }
        
        response.setContentType("text/html");
        HttpServletResponseWrapper responseWrapper = new HttpServletResponseWrapper(response);
        Writer out = responseWrapper.getWriter();
        StringBuilder message = new StringBuilder();
        /*
        message.append("Message ");
        message.append(msg);
        message.append(" Keyword: ");
        message.append(keyword);
        message.append(" organizationId: ");
        message.append(organizationId);
        message.append(" tokenNo ");
        message.append(tokenNo);
        */
        message.append(" Current Position: ");
        message.append(statusModel.getTokenPositionInQueue());
        message.append(" TKN No.: ");
        message.append(statusModel.getTokenNo());
        message.append(" Status: ");
        message.append(statusModel.getStatus());
        message.append(" Name: ");
        message.append(statusModel.getName());
        message.append(" ORG: ");
        message.append(statusModel.getCompanyName());
        message.append(" (");
        message.append(statusModel.getDestinationName());
        message.append(")");
        message.append(" Sent By: ");
        message.append(statusModel.getFrom());
        out.write(message.toString());
        out.close();
        
        System.out.println("Reply Message: "+message);
        //return new ModelAndView("statusReply", "status", statusModel);
    }
    
    @RequestMapping(value="replySmsCountry", method= RequestMethod.GET)
    public void replySmsCountry(ModelMap model, HttpServletRequest request,
                                              HttpServletResponse response) throws IOException{
        
        String mobileNo = null;        
        String message = null;
        Date receivedon = null;
        Long organizationId = 0L;
        Integer tokenNo = 0;
        
        if(request.getParameter("message") != null){
            message = (String)(request.getParameter("message"));
            String messageArray[] = message.split(" ");
            organizationId = new Long(messageArray[0]);
            tokenNo = new Integer(messageArray[1]);
        }
        if(request.getParameter("mobilenumber") != null){
            mobileNo = (String)(request.getParameter("mobilenumber"));
        }
        if(request.getParameter("receivedon") != null){
            String dateString = (String)(request.getParameter("receivedon"));
            //receivedon = new Date(dateString);
        }
        
        System.out.println("OrganizationId: "+organizationId);
        System.out.println("Token No: "+tokenNo);
        
        
        TokenStatusReplyModel statusModel = tokenService.tokenReply(organizationId, tokenNo);
        if(statusModel.getTokenNo() != null){
            //return new ModelAndView("statusReply", "status", statusModel);
        }
        else{
            statusModel.setAdditionalMessage("Not in queue");
            //return new ModelAndView("statusReply", "status", statusModel);
        }
        
        response.setContentType("text/html");
        HttpServletResponseWrapper responseWrapper = new HttpServletResponseWrapper(response);
        Writer out = responseWrapper.getWriter();
        StringBuilder messageBuilder = new StringBuilder();
        messageBuilder.append("TKN No.: ");
        messageBuilder.append(statusModel.getTokenNo());
        messageBuilder.append(" Status: ");
        messageBuilder.append(statusModel.getStatus());
        messageBuilder.append(" Name: ");
        messageBuilder.append(statusModel.getName());
        messageBuilder.append(" at ");
        messageBuilder.append(statusModel.getCompanyName());
        messageBuilder.append(" for ");
        messageBuilder.append(statusModel.getDestinationName());
        messageBuilder.append(" Token Position: ");
        messageBuilder.append(statusModel.getTokenPositionInQueue());
        messageBuilder.append(" From: ");
        messageBuilder.append(statusModel.getFrom());
        out.write(messageBuilder.toString());
        out.close();
        
        System.out.println("Reply Message: "+messageBuilder);
        //return new ModelAndView("statusReply", "status", statusModel);
    }
    
}
